import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import { isLogined } from '../utils/tools'   //路由守卫

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/music',
    name: 'Music',
    component: () => import('../views/Music.vue')
  },
  {
    path: '/friend',
    name: 'Friend',
    component: () => import('../views/Friend.vue')
  },
  {
    path: '/user',
    name: 'User',
    meta: {
      needLogin: true
    },
    component: () => import('../views/User.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/reg',
    name: 'Reg',
    component: () => import('../views/Reg.vue')
  },
  {
    path: '/play',
    name: 'Play',
    component: () => import('../views/Play.vue')
  },
  {
    path: '/play',
    name: 'Play',
    component: () => import('../views/Play.vue')
  },
  {
    path: '/sign',
    name: 'Sign',
    component: () => import('../views/Sign.vue')
  },
  {
    path: '/password',
    name: 'Password',
    component: () => import('../views/Password.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {  //路由守卫
  if (to.meta.needLogin) {
    if (isLogined()) {
      next()
    } else {
      next({
        name: "Login",
      })
    }
  } else {
    next()
  }
})

export default router
